package no.gnet.s10.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.util.StringTokenizer;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import no.gnet.s10.types.GenericTransferBean;
import no.gnet.util.Coder;
import no.gnet.util.Constants;
import no.gnet.util.ErrorMessage;

/**
 * Servlet implementation class for Servlet: Controller
 *
 */
 public class PlayerCreator extends GNetController{

	 /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public PlayerCreator() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		handleCreation(request, response);
		
	}

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		handleCreation(request, response);
	}  	
	
	private void handleCreation(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String command = request.getParameter("cmd");
			//Create
			if(!command.equalsIgnoreCase("activate")){
				handleRequest(request, response, command);				
			}
			else{//activate
				String ident = request.getParameter("ident");
				String decoded = new String(Coder.decode(ident));
				StringTokenizer st = new StringTokenizer(decoded,"|");
				String handle = st.nextToken();
				activatePlayer(handle);
				String message = handle+", din konto er aktivert!";
				request.getSession().setAttribute("message", message);
				getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
		
}